import random

class NumberGuessingGame:
    def __init__(self, min_num=1, max_num=100, max_attempts=10):
        self.min_num = min_num
        self.max_num = max_num
        self.max_attempts = max_attempts
        self.secret_number = None
        self.attempts = 0
        self.guessed_correctly = False

    def start(self):
        """开始新游戏"""
        self.secret_number = random.randint(self.min_num, self.max_num)
        self.attempts = 0
        self.guessed_correctly = False
        print(f"我想了一个{self.min_num}到{self.max_num}之间的数字，你有{self.max_attempts}次机会猜出它。")
        self._play_round()
        '''重复玩耍'''
        while 1:
            self.choice = input("你还想玩么？y/n")
            if self.choice == 'y':
                self.start()
            elif self.choice == 'n':
                return
            else:
                print('请重新输入你的选择：y/n')
            


    def _play_round(self):
        """处理一轮猜测"""
        while self.attempts < self.max_attempts and not self.guessed_correctly:
            self.attempts += 1
            guess = self._get_valid_guess()
            result = self._evaluate_guess(guess)
            print(result)
            
            if self.guessed_correctly:
                print(f"恭喜！你用了{self.attempts}次尝试就猜对了。")
                return
            elif self.attempts < self.max_attempts:
                remaining = self.max_attempts - self.attempts
                print(f"还剩{remaining}次尝试。")
        
        if not self.guessed_correctly:
            print(f"游戏结束！正确答案是{self.secret_number}。")

    def _get_valid_guess(self) -> int:
        """获取有效输入"""
        while True:
            try:
                guess = int(input(f"第{self.attempts}次猜测: "))
                if self.min_num <= guess <= self.max_num:
                    return guess
                else:
                    print(f"请输入{self.min_num}到{self.max_num}之间的数字。")
            except ValueError:
                print("请输入有效的整数。")

    def _evaluate_guess(self, guess: int) -> str:
        """评估猜测结果"""
        if guess < self.secret_number:
            return "太小了！"
        elif guess > self.secret_number:
            return "太大了！"
        else:
            self.guessed_correctly = True
            return "猜对了！"

# 运行示例
if __name__ == "__main__":
    game = NumberGuessingGame(1,100,5)
    game.start()